﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Dtos;


namespace Production_Set.API.Application.Commands.Procurement
{
    /// <summary>
    /// 审批
    /// </summary>
    public class ApprovalStatusHandler : IRequestHandler<ApprovalStatusCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单头信息
        /// </summary>
        private readonly IBaseRepository<Purchasings> purchasingsRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ApprovalStatusHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="logger"></param>
        public ApprovalStatusHandler(IBaseRepository<Purchasings> purchasingsRepository, ILogger<ApprovalStatusHandler> logger)
        {
            this.purchasingsRepository = purchasingsRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(ApprovalStatusCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Update };


            string sql = $"UPDATE Purchasings SET ApprovalStatus=@ApprovalStatus where PurchasinId={request.PurchasinId}";
            var list = purchasingsRepository.Update(sql, new
            {
                ApprovalStatus = request.ApprovalStatus + 1
            });
            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Msg = "修改成功";
                result.Msg = "审批成功，操作人：" + request.name;
                logger.LogInformation("审批成功，操作人：" + request.name);
            }

            return result;
        }
    }
}
